﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.Common;

using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Web.Security;
using Husb.DataUtil;

namespace CentralOffice.WebClientTest
{
    public partial class _Default : System.Web.UI.Page
    {
        private const string Select_App = "SELECT top 20 Id, ApplicationId, ApprovedBy, DateApproved, ContentText, Opinion FROM Tasks.Approvals order by ModifiedTime desc";
        private const string Select_Log = "SELECT top 20 LogID, Message, FormattedMessage FROM Admin.Log  ORDER BY Timestamp DESC ";
        private const string Select_Task = "SELECT top 20 Id, Name FROM Tasks.Tasks order by ModifiedTime desc";

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnExecuteSQL_Click(object sender, EventArgs e)
        {
            if (txtSQL.Text.Length == 0) return;
            if (txtSQL.Text.Trim().ToLower().StartsWith("select") || txtSQL.Text.Trim().ToLower().StartsWith("exec"))
            {
                DataBound(txtSQL.Text.Trim());
            }
            
        }

        protected void btnExeLog_Click(object sender, EventArgs e)
        {
            DataBound(Select_Log);
        }

        protected void btnExeTask_Click(object sender, EventArgs e)
        {
            DataBound(Select_Task);
        }

        protected void btnExeApp_Click(object sender, EventArgs e)
        {
            DataBound(Select_App);
        }

        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();


            LogWriter.WriteLogEntry(Husb.Util.LogType.SignOn, ApplicationContext.ActualUserName + "于 " + DateTime.Now.ToString() + " 通过测试站点退出本系统。");
          
        }

        private static DataTable ExecuteDataTable(string sql)
        {
            DataTable dt = null;
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            try
            {
                DataSet ds = db.ExecuteDataSet(cmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    dt = ds.Tables[0];
                }
            }
            catch
            {
                throw;
            }
            finally
            {

            }
            return dt;
        }

        private void DataBound(string sql)
        {
            gv.DataSource = ExecuteDataTable(sql);
            gv.DataBind();
        }
    }
}
